package ch18_filter.web;

import java.util.ArrayList;
import java.util.List;

/**
 * 过滤器链
 *
 * @author guodd
 * @version 1.0
 */
public class FilterChain implements Filter {
    private List<Filter> filters = new ArrayList<>();
    private int index = 0;

    public FilterChain add(Filter filter) {
        this.filters.add(filter);
        return this;
    }


    @Override
    public void doFilter(Request request, Response response, FilterChain chain) {
        if (index == filters.size()) return;
        Filter f = filters.get(index);
        index++;
        f.doFilter(request, response, chain);
    }
}
